package com.dbq.share;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;

import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;

import java.util.concurrent.ConcurrentHashMap;

import static com.dbq.share.SocializeMediaConstant.COPY;
import static com.dbq.share.SocializeMediaConstant.FACEBOOK;
import static com.dbq.share.SocializeMediaConstant.QQ;
import static com.dbq.share.SocializeMediaConstant.QZONE;
import static com.dbq.share.SocializeMediaConstant.SINA;
import static com.dbq.share.SocializeMediaConstant.WEIXIN;
import static com.dbq.share.SocializeMediaConstant.WEIXIN_TIMELINE;

/**
 * Created by sunkai on 2017/12/26.
 */

class YibaShare {

    ConcurrentHashMap<String, ShareCallback> shareCallback = new ConcurrentHashMap<>();

    public void share(Context context, ShareTarget item, ShareWebPage shareWebPage, String uuid) {
        if (item == null || shareWebPage == null) {
            //TODO
            return;
        }

        switch (item.media) {
            case SINA:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_WEIBO);
                YibaShareDelegateActivity.start(context, item, shareWebPage, uuid);
                break;
            case WEIXIN:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_WECHAT);
                YibaShareDelegateActivity.start(context, item, shareWebPage, uuid);
                break;
            case WEIXIN_TIMELINE:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_PENGYOUQUAN);
                YibaShareDelegateActivity.start(context, item, shareWebPage, uuid);
                break;
            case QQ:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_QQ);
                YibaShareDelegateActivity.start(context, item, shareWebPage, uuid);
                break;
            case QZONE:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_QQZONE);
                YibaShareDelegateActivity.start(context, item, shareWebPage, uuid);
                break;
            case FACEBOOK:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_FACEBOOK);
                YibaShareDelegateActivity.start(context, item, shareWebPage, uuid);
                break;
            case COPY:
                YibaAnalysis.getInstance().event(context, EventConstant. SHARE_BY_LINK);
                copyLink(context, shareWebPage, uuid);
                break;
        }


    }

    private void copyLink(Context context, ShareWebPage shareWebPage, String uuid) {

        ClipboardManager clip = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (BuildHelper.isApi11_HoneyCombOrLater()) {
            clip.setPrimaryClip(ClipData.newPlainText(null, shareWebPage.url));
        } else {
            ((android.text.ClipboardManager) clip).setText(shareWebPage.url);
        }
        Toast.makeText(context, R.string.dbq_share_sdk_share_copy, Toast.LENGTH_SHORT).show();
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.shareComplete(COPY);
            YibaShare.getShareClient().shareCallback.remove(uuid);
        }

    }

    private void shareToSina(ShareWebPage shareWebPage) {

    }

    private static final class SingleHolder {
        private final static YibaShare yibaShare = new YibaShare();
    }

    private YibaShare() {
    }


    public static YibaShare getShareClient() {
        return SingleHolder.yibaShare;
    }
}
